﻿#region NameSpace
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Configuration;
using System.Web.UI;
using System.Web;
using System.Web.UI.WebControls;
using ebird.eBirdGenerics;
using ebird.StoredProcExecution;
#endregion

namespace EB.Generics 
{
   
   public class Generics
   {
       #region Fields
       string connectionString = string.Empty;
       SqlCommand command;
       SqlConnection connection;
       DataSet dataSet;
       SqlDataAdapter sqlDataAdapter;
       #endregion

       #region Constructor
       /// <summary>
       /// Connection to database
       /// </summary>
       public Generics()
       {

            connectionString = ConfigurationManager.ConnectionStrings["connectUserManager"].ToString();
           //connectionString="Data Source=ODYSSEY\\SQLEXPRESS;Initial Catalog=EBBusinessProcessAutomation;User Id=sa;Password=bmjo";

           connection = new SqlConnection(connectionString);
       }

       #endregion

       #region Methods
       /// <summary>
       /// returns number of rows affected (Use Insert/Update/Delete statement)
       /// </summary>
       /// <returns></returns>
       public int ExecuteNonQuery(string query)
       {
           int count = 0;
           command = new SqlCommand();
           command.Connection = connection;
           command.CommandType = CommandType.Text;
           command.CommandText = query;
           connection.Open();
           count = command.ExecuteNonQuery();
           connection.Close();
           return count;
       }

       /// <summary>
       /// returns DataSet(Use Select Statement)
       /// </summary>
       /// <param name="query"></param>
       /// <returns></returns>
       public DataSet ExecuteQuery(string query)
       {
           command = new SqlCommand();
           dataSet = new DataSet();
           command.Connection = connection;
           command.CommandText = query;
           connection.Open();
           sqlDataAdapter = new SqlDataAdapter(command);
           sqlDataAdapter.Fill(dataSet);
           connection.Close();
           return dataSet;
       }
       /// <summary>
       /// returs single value(1st Column of 1st row)
       /// </summary>
       /// <param name="query"></param>
       /// <returns></returns>
       public object ExecuteScalar(string query)
       {
           Object count;
           command = new SqlCommand();
           command.Connection = connection;
           command.CommandType = CommandType.Text;
           command.CommandText = query;
           connection.Open();
           count =command.ExecuteScalar();
           connection.Close();
           return count;
       }

       public void PopulateGridView(GridView grvName,string query)
       {
           DataSet gridDS = new DataSet();
           gridDS = ExecuteQuery(query);
           grvName.DataSource = gridDS;
           grvName.DataBind();
       }
       public void PopulateCombo(DropDownList ddlName, string query,string dataValueID,string dataText)
       {
           DataSet comboDS = new DataSet();
           comboDS = ExecuteQuery(query);
           ddlName.DataSource = comboDS;
           ddlName.DataValueField = "VendorID";
           ddlName.DataTextField = "VR_VendorName";
           ddlName.DataBind();
       }
       public void PopulateCombo(DropDownList ddlName,string procedureName)
       {
           ebird.eBirdGenerics.Generics genericFun = new ebird.eBirdGenerics.Generics(connectionString);
           StoredProcedureCollection spCollection=new StoredProcedureCollection();
           StoredProcedure spData=new StoredProcedure();
           spData.ProcName = procedureName;
           //spData.SetParam("@ProductName", SqlDbType.VarChar, "kk");
           spCollection.add(spData);
           DataSet spDS = genericFun.ExecuteQuery(spCollection);
           for (int count = 0; count < spDS.Tables[0].Rows.Count; count++)
           {
               ddlName.Items.Add(spDS.Tables[0].Rows[count].ItemArray.GetValue(1).ToString());
               ddlName.Items[count].Value = spDS.Tables[0].Rows[count].ItemArray.GetValue(0).ToString();
           }
           ddlName.Items.Insert(0, "Select");
           ddlName.SelectedIndex = 0;

       }

       public void PopulateGridView(GridView grdName,string procedureName,string condition)
       {
           ebird.eBirdGenerics.Generics genericFun = new ebird.eBirdGenerics.Generics(connectionString);
           StoredProcedureCollection spCollection = new StoredProcedureCollection();
           StoredProcedure spData = new StoredProcedure();
           spData.ProcName = procedureName;
            spData.SetParam("@Condition",SqlDbType.Text,condition);
           spCollection.add(spData);
           DataSet gridDS = genericFun.ExecuteQuery(spCollection);
           grdName.DataSource = gridDS;
           grdName.DataBind();
       }
       public SqlDataAdapter SQLDataAdapter()
       {
           sqlDataAdapter = new SqlDataAdapter("", connection);
           return sqlDataAdapter;
       }
       #endregion
   }

   public class AddTemplateToGridView : ITemplate
   {
       ListItemType liType;
       string strColName, strNavigateUrl;

       public AddTemplateToGridView(ListItemType type, string strColumn, string strNavigate)
       {
           liType = type;
           strColName = strColumn;
           strNavigateUrl = strNavigate;
       }

       void ITemplate.InstantiateIn(System.Web.UI.Control container)
       {
           switch (liType)
           {
               case ListItemType.Item:

                   //HyperLink hyLink = new HyperLink();
                   //hyLink.Target = "_blank";

                   //hyLink.DataBinding += new EventHandler(BindDataBinding);
                   //container.Controls.Add(hyLink);
                   LinkButton hyLink = new LinkButton();

                   //hyLink.Target = "_blank";

                   hyLink.DataBinding += new EventHandler(BindDataBinding);
                   container.Controls.Add(hyLink);
                   break;
           }
       }
       /// <summary>
       /// BindDataBinding
       /// To set the navigate url of template column       
       /// <returns> </returns>
       /// <Created By="Thushara" on="06-11-2007" />
       /// </summary>
       void BindDataBinding(object sender, EventArgs e)
       {
           //HyperLink hLink = (HyperLink)sender;
           //GridViewRow container = (GridViewRow)hLink.NamingContainer;
           //object dataValue = DataBinder.Eval(container.DataItem, strColName);
           //if (dataValue != DBNull.Value)
           //{
           //    hLink.Text = dataValue.ToString();
           //    //strNavigateUrl.Replace('+', '&');
           //    hLink.NavigateUrl = strNavigateUrl;
           //}
           LinkButton hLink = (LinkButton)sender;
           GridViewRow container = (GridViewRow)hLink.NamingContainer;
           object dataValue = DataBinder.Eval(container.DataItem, strColName);
           if (dataValue != DBNull.Value)
           {
               hLink.Text = dataValue.ToString();
               //strNavigateUrl.Replace('+', '&');
               //hLink.NavigateUrl = strNavigateUrl;
           }
       }
   }
}



